<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/4/2 0002
 * Time: 16:21
 */

namespace app\admin\controller;

use app\common\model\rbac\RbacAdminModel;
use app\common\model\rbac\RbacRoleModel;
use app\common\widget\WidgetDataListTable;
use app\common\widget\WidgetSearchForm;


/**
 * 权限管理模块
 * Class Rbac
 * @package app\admin\controller
 */
class Rbac extends AdminCommon
{
    /**
     * 初始化方法
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    //****************************************管理员Start******************************************//

    /**
     * 管理员列表
     * @throws \Exception
     */
    public function admin_user_list() {
        // 搜索条件
        $search = $this->parse_search();
        // admin model
        $adminModel = RbacAdminModel::getInstance();
        // role model
        $roleModel = RbacRoleModel::getInstance();

        // 查询条件
        $map = [
            'id' => ['neq', 1],
        ];
        if ( $search ) {
            isset($search['id']) && $adminModel->where("id", "eq", $search['id']) ;
        }
        // search form
        $searchForm = new WidgetSearchForm();
        // ID
        $searchForm->addTextInput()->setId('id')->setName('id')->setPlaceholder('请填写管理员编号')->setValue($search['id']??'');
        // assign tpl
        $this->assign('search_form', $searchForm->render());

        // 数据列表
        $admin_data_list = [];
        $role_name_list = [];
        $admin_id_list = $adminModel->search_id_list( $map, $this->page );
        if ( $admin_id_list ) $admin_data_list = $adminModel->muti_get_data_by_id_list( $admin_id_list );
        if ( $admin_data_list ) {
            $role_id_list = $admin_data_list ? array_column($admin_data_list, "role_id") : array();
            $role_name_list = $role_id_list ? $roleModel->get_key_value('id', 'name', ['id'=>['in', $role_id_list]]) : [];

        }

        $fieldTitle = [
            'id' => 'ID',
            'name' => '用户名',
            'nick_name' => '昵称',
            'real_name' => '真实姓名',
            'role_id' => '角色',
            'email' => '邮箱',
            'mobile_phone' => '手机号',
            'status' => '状态',
            'last_login_time' => '登陆时间',
            'create_time' => '创建时间',
        ];
        $dataTable = new WidgetDataListTable();
        $dataTable->setDataList($admin_data_list)->setColumnFieldTitle($fieldTitle)->setPageList($adminModel->page_list);
        $dataTable->addColumn("edit", "操作");
        $dataTable->setColumnFieldDependence("role_id", $role_name_list);

        $this->assign('table', $dataTable->render());
        return $this->fetch( __FUNCTION__ );
    }

    //****************************************管理员End******************************************//

    //****************************************节点控制Start******************************************//

    /**
     * 节点列表
     */
    public function node_list() {
        $fieldTitle = [
            'id' => '编号',
            'group' => '分组',
            'name' => '名称',
            'title' => '标题',
            'status' => '状态',
            'remark' => '备注',
            'icon' => '图标',
            'sort' => '排序',
            'pid' => '父编号',
            'level' => '等级 项目1 模块2 方法3',
            'path' => '排序路径',
            'is_show_btn' => '是否在后台显示点击 1:显示 0:不显示',
            'is_default' => '是否为默认权限 1:是 0:不是',
        ];
    }

    //****************************************节点控制End******************************************//
}